﻿Public Class FrmCopyIncome
    Dim dsCpInc As New DataSet
    Dim daCpInc As OleDb.OleDbDataAdapter
    Dim sql As String

    Private Sub FrmCopyIncome_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub

    Private Sub FrmCopyIncome_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Call gSubIconShow(Me)
        Call gSubIconShow(Me)
        Call mkCmbIncMonth()
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        If dpIncomeDt.Text = "" Then
            MsgBox("거래일을 지정해 주세요")
            Exit Sub
        End If

        Dim result As Integer = MessageBox.Show("매출복사를 진행하시겠습니까?", "매출 복사", MessageBoxButtons.YesNo)
        If result = DialogResult.No Then
            Exit Sub
        ElseIf result = DialogResult.Yes Then
            Call copyIncome(cmbMonth.SelectedValue.ToString, dpIncomeDt.Text)
        End If
        MsgBox("매출복사가 완료 되었습니다")
    End Sub

    Private Sub cmbMonth_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbMonth.KeyPress
        e.Handled = True
    End Sub

#Region "프로시져"

    '## 복사 하기
    Private Sub copyIncome(ByVal stdMnth As String, ByVal targetDt As String)

        Dim valIncCd As String
        Dim curDt As DateTime = Now

        Call dbConnection()
        sql = "SELECT compCd, article, accountCd, amount, orgAmount, tax  FROM tblIncome WHERE LEFT(incomeDt,7)='" + stdMnth + "'"
        dsCpInc = New DataSet
        daCpInc = New OleDb.OleDbDataAdapter(sql, dbConn)
        daCpInc.Fill(dsCpInc, "mnthIncome")

        For k = 0 To dsCpInc.Tables("mnthIncome").Rows.Count - 1

            valIncCd = gFnGetIncCd()
            sql = "INSERT INTO tblIncome(incCd, compCd, article, accountCd, amount, orgAmount, tax, restAmount, incomeDt, addDt) " + _
                    "VALUES('" + valIncCd + "', '" + dsCpInc.Tables("mnthIncome").Rows(k).Item(0).ToString + "'" + _
                    ", '" + dsCpInc.Tables("mnthIncome").Rows(k).Item(1).ToString + "'" + _
                    ", '" + dsCpInc.Tables("mnthIncome").Rows(k).Item(2).ToString + "'" + _
                    ", '" + dsCpInc.Tables("mnthIncome").Rows(k).Item(3).ToString + "'" + _
                    ", '" + dsCpInc.Tables("mnthIncome").Rows(k).Item(4).ToString + "'" + _
                    ", '" + dsCpInc.Tables("mnthIncome").Rows(k).Item(5).ToString + "'" + _
                    ", '" + dsCpInc.Tables("mnthIncome").Rows(k).Item(3).ToString + "'" + _
                    ", '" + targetDt + "', '" + curDt + "')"
            Call gSubSaveAction(sql, "")
        Next

        dbConn.Close()
    End Sub

    '## 자료 보유 년도
    Private Sub mkCmbIncMonth()

        Call dbConnection()
        sql = "SELECT top 12 LEFT(incomeDt,7) as mnth FROM tblIncome WHERE flag='Y' GROUP BY LEFT(incomeDt,7) ORDER BY LEFT(incomeDt,7) DESC"
        dsCpInc = New DataSet
        daCpInc = New OleDb.OleDbDataAdapter(sql, dbConn)
        daCpInc.Fill(dsCpInc, "cmbIncMonth")

        dbConn.Close()
        With cmbMonth
            .DataSource = dsCpInc.Tables("cmbIncMonth")
            .DisplayMember = "mnth"
            .ValueMember = "mnth"
        End With
        cmbMonth.SelectedIndex = 0
    End Sub
#End Region
End Class